package cn.doctorpda.conference.ui.personal.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

import java.io.File;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.presenter.LogoutPresent;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IBaseView;
import okhttp3.Call;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class PersonalSettingDialogFragment extends DialogFragment implements
        OnClickListener, IBaseView<Msg> {

    private FragmentActivity mActivity;
    private LogoutPresent present;

    private OnLogoutListener listener;

    private File outDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    public static String picFileFullName;


    public void setLogoutListener(OnLogoutListener listener) {
        this.listener = listener;
    }

    public interface OnLogoutListener {
        void setLogoutListener(Msg msg);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_personal_setting, null);

        inflate.findViewById(R.id.btn_logout).setOnClickListener(this);
        inflate.findViewById(R.id.btn_close).setOnClickListener(this);
        inflate.findViewById(R.id.btn_change_psw).setOnClickListener(this);
        inflate.findViewById(R.id.btn_change_personal_data).setOnClickListener(this);
        inflate.findViewById(R.id.btn_change_phonenumber).setOnClickListener(this);
        inflate.findViewById(R.id.btn_camera).setOnClickListener(this);
        inflate.findViewById(R.id.btn_photo).setOnClickListener(this);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        getDialog().setCanceledOnTouchOutside(false);

        present = new LogoutPresent(this);
        return inflate;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.btn_logout:
                present.logout();

                break;
            case R.id.btn_change_personal_data:
                ChangeNameDialogFragment changeNameDialogFragment = new ChangeNameDialogFragment();
                changeNameDialogFragment.show(getActivity().getSupportFragmentManager(), "alertName");
                break;
            case R.id.btn_change_psw:
                ChangePswDialogFragment cDialogFragment = new ChangePswDialogFragment();
                cDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;
            case R.id.btn_change_phonenumber:
                ChangeMobileDialogFragment changeMobileDialogFragment = new ChangeMobileDialogFragment();
                changeMobileDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;

            case R.id.btn_camera:
                callCamera();
                break;

            case R.id.btn_photo:
                callAlbum();
                break;

            case R.id.btn_close:

                break;

            default:
                break;
        }
    }


    @Override
    public void setResult(Msg msg, Msg data) {

        listener.setLogoutListener(msg);
    }

    // 调用相机
    public void callCamera() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            if (!outDir.exists()) {
                outDir.mkdirs();
            }
            File outFile = new File(outDir, System.currentTimeMillis() + ".jpg");
            picFileFullName = outFile.getAbsolutePath();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile));
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            getActivity().startActivityForResult(intent, AppConfig.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        } else {
            // Log.e(tag, "请确认已经插入SD卡");
            System.out.println("请确认已经插入SD卡");
            T.showShort(getContext(),"请确认已经插入SD卡");
        }

    }
    // 调用相册
    public void callAlbum() {

        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        getActivity().startActivityForResult(intent, AppConfig.ALBUM_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    @Override
    public void setError(Call call, Exception e) {

    }
}